package com.web.jdbcbase.controller;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.base.BaseController;
import com.web.jdbcbase.module.Menu;
import com.web.jdbcbase.service.MenuService;
import com.web.jdbcyw1.module.User;
import com.web.jdbcyw1.service.UserService;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController<Menu>{
	@Resource//注入url2数据源service
	private MenuService menuService;
	
	@Resource//注入url数据源sevice
	private UserService userService;
	
	@RequestMapping("/getUserList.do")
	@ResponseBody
	public List<User> queryUser() {
		User u = new User();
		return menuService.queryUser();
	}
	
	//http://localhost:9081/springMvc/menu/getMenuList.br?menuIds=1ddS2&menuIds=1AA2&orderNo=1&menuId=bcd3e794-e032-406d-a415-7b2b20b4df88
	@RequestMapping("/getMenuList.br")
	@ResponseBody
	public List<Menu> getMenuList(){
		Menu menu = this.getModule("a","b");
		List<Menu> list = menuService.queryMenu(menu);
		return list;
	}
	@RequestMapping("/error.br")
	public String error(){
		return "error";
	}
	@RequestMapping(method = RequestMethod.GET)//声明此方法是该控制器的默认service方法http://localhost:8080/springMvc/menu/
	@ResponseBody
	public List<Menu> getMenuList2(){
		Menu menu = new Menu();
		menu.setOrderNo(1);
		menu.setMenuId("bcd3e794-e032-406d-a415-7b2b20b4df88");
		List<Menu> list = menuService.queryMenu(menu);
	//	System.out.println(1/0);
		return list;
	}
	@RequestMapping("/getMenuListMap.br")
	@ResponseBody
	public List<Map> getMenuListMap(){
		Menu menu = new Menu();
		menu.setOrderNo(1);
		menu.setMenuId("bcd3e794-e032-406d-a415-7b2b20b4df88");
		List<Map> list = menuService.queryMenuMap(menu);
		return list;
	}
	@RequestMapping("/getMenuForOne.htm")
	@ResponseBody
	public Menu getMenuForOne(){
		String str = "";
		Menu menu2 = menuService.queryMenuForOne(str);
		return menu2;
	}
	@RequestMapping("/addUser")
	@ResponseBody
	public int addUser() {
		User user = new User();
		user.setName("test");
		user.setPwd("pwd的都");
		return menuService.addUser(user);
	}
	@RequestMapping("/addMenu")
	@ResponseBody
	public int addMenu() {
		System.out.println(1+"===============");
		Menu menu = new Menu();
		menu.setMenuId("11111111111");
		menu.setMenuName("tests是");
		return menuService.addMenu(menu);
	}
	//http://localhost:9081/springMvc/menu/addUserAndMenu
	@RequestMapping("/addUserAndMenu")
	@ResponseBody
	public int addUserAndMenu(){
		User user = new User();
		user.setName("testddw");
		user.setPwd("pwd的都ddw");
		
		Menu menu = new Menu();
		menu.setMenuId("11111111111ddw");
		menu.setMenuName("tests是ddw");
		return menuService.addUserAndMenu(user,menu);
	}
	/**
	 * @desc 导出文件测试
	 * @author wdd-think
	 */
	@RequestMapping(value="exportFile.htm")
	@ResponseBody
	public void exportFile(HttpServletResponse response){
		try {
			byte[] buff=new byte[]{};  
			String aa="test文件内容";  
			buff=aa.getBytes();  
//			HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.addHeader("Content-Disposition", "attachment;filename=" + new String("文件名.txt".getBytes("gbk"), "ISO8859-1"));
			response.setCharacterEncoding("utf8");
			response.setContentType("application/octet-stream");
			toClient.write(buff);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
